import fire from '@dfeidao/atom-web/msg/fire';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import render from '@dfeidao/atom-web/render/render';
import set_node_cls from '@dfeidao/atom-web/ui/set-node-cls';

import { IFeidaoAiBrowserComponent } from '@dfeidao/web/interfaces';
import hide_loading from '../../atom/hide-loading';
import show_loading from '../../atom/show-loading';
import P03 from './P03';

import get from '@dfeidao/atom-web/local/get';
import remove from '@dfeidao/atom-web/local/remove';
import set from '@dfeidao/atom-web/local/set';

export default async function a002(fd: IFeidaoAiBrowserComponent, args: Event) {
	const nodea = args.currentTarget as HTMLDivElement;
	const node = nodea.closest('[tlist]') as HTMLDivElement;
	const nodes = fd.data.node.querySelectorAll<HTMLDivElement>('[tlist]');

	const productid = get(fd, 'productid');
	nodes.forEach((i) => {
		set_node_cls(i, 'bg-c', false);

	});
	set_node_cls(node, 'bg-c', true);
	const id = node.getAttribute('data-feidao-id2');
	const connector = node.getAttribute('data-feidao-connector');
	const group_id = node.getAttribute('data-feidao-group_id');
	const group = node.getAttribute('data-feidao-group');
	const allot_group = {
		id, connector, group_id, group
	};
	set(fd, 'allot_group', allot_group);
	remove(fd, 'shezhi');
	const role_allot_id = get(fd, 'role_allot_id');
	const query = {
		productid, role_allot_id, group_id
	};
	show_loading();
	const res = await nodejs<{ dataArray: object[], length: number }>('roler/zj-000004/s008', query);
	hide_loading();
	if (res.length === 0) {
		render(fd, '', P03, 'P03', 'inner');
	} else {
		const data = res.dataArray.map((v) => {
			return { ...v, v: JSON.stringify(v) };
		});
		render(fd, data, P03, 'P03', 'inner');
	}
	fire('zj-000004', 'eu-013', query);
}
